using HKB.Common;
using System;
using System.Data;
namespace HKB.Web.DataHelper
{
	public class CM_AD_Manage
	{
		private Conn conn;
		public CM_AD_Manage()
		{
			this.conn = new Conn();
		}
		public CM_AD_Manage(Conn _conn)
		{
			this.conn = _conn;
		}
		public bool IsExistAd(string AdLocation, string City, string Area, int SortClassID)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@AdLocation", DbType.String, AdLocation),
				this.conn.CreateParameter("@City", DbType.String, City),
				this.conn.CreateParameter("@Area", DbType.String, Area),
				this.conn.CreateParameter("@SortClassID", DbType.Int32, SortClassID)
			};
			return this.conn.ExecuteCount("select COUNT(*) from CM_Advertisement\r\n                                where IsDel=1 and EndTime>GETDATE() and AdLocation=@AdLocation\r\n                                and City=@City and Area=@Area and SortClassID=@SortClassID ", parameters) > 0;
		}
		public bool UpdateAdvertisment(string ad_id, string adName, string location, string province, string city, string area, int sortID, DateTime beginTime, DateTime endTime, string linkUrl, string linkTip, string imgUrl, string adIntro, int enable)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@AdID", DbType.Int32, ad_id),
				this.conn.CreateParameter("@AdName", DbType.String, adName),
				this.conn.CreateParameter("@AdLocation", DbType.String, location),
				this.conn.CreateParameter("@Province", DbType.String, province),
				this.conn.CreateParameter("@City", DbType.String, city),
				this.conn.CreateParameter("@Area", DbType.String, area),
				this.conn.CreateParameter("@SortClassID", DbType.Int32, sortID),
				this.conn.CreateParameter("@BeginTime", DbType.DateTime, beginTime),
				this.conn.CreateParameter("@EndTime", DbType.DateTime, endTime),
				this.conn.CreateParameter("@LinkUrl", DbType.String, linkUrl),
				this.conn.CreateParameter("@LinkTip", DbType.String, linkTip),
				this.conn.CreateParameter("@ImgPath", DbType.String, imgUrl),
				this.conn.CreateParameter("@AdIntro", DbType.String, adIntro),
				this.conn.CreateParameter("@Enabled", DbType.Int32, enable)
			};
			return this.conn.Execute("UPDATE [CM_Advertisement]\r\n                                   SET [AdName] = @AdName\r\n                                      ,[AdLocation] = @AdLocation\r\n                                      ,[ImgPath] = @ImgPath\r\n                                      ,[AdIntro] = @AdIntro\r\n                                      ,[LinkUrl] = @LinkUrl\r\n                                      ,[LinkTip] = @LinkTip\r\n                                      ,[Province] = @Province\r\n                                      ,[City] = @City\r\n                                      ,[Area] = @Area\r\n                                      ,[SortClassID] = @SortClassID\r\n                                      ,[Enabled] = @Enabled\r\n                                      ,[BeginTime] = @BeginTime\r\n                                      ,[EndTime] = @EndTime\r\n                                 WHERE AdID=@AdID ", parameters) > 0;
		}
		public bool AddAdvertisment(string adName, string location, string province, string city, string area, int sortID, DateTime beginTime, DateTime endTime, string linkUrl, string linkTip, string imgUrl, string adIntro, int enable)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@AdName", DbType.String, adName),
				this.conn.CreateParameter("@AdLocation", DbType.String, location),
				this.conn.CreateParameter("@Province", DbType.String, province),
				this.conn.CreateParameter("@City", DbType.String, city),
				this.conn.CreateParameter("@Area", DbType.String, area),
				this.conn.CreateParameter("@SortClassID", DbType.Int32, sortID),
				this.conn.CreateParameter("@BeginTime", DbType.DateTime, beginTime),
				this.conn.CreateParameter("@EndTime", DbType.DateTime, endTime),
				this.conn.CreateParameter("@LinkUrl", DbType.String, linkUrl),
				this.conn.CreateParameter("@LinkTip", DbType.String, linkTip),
				this.conn.CreateParameter("@ImgPath", DbType.String, imgUrl),
				this.conn.CreateParameter("@AdIntro", DbType.String, adIntro),
				this.conn.CreateParameter("@Enabled", DbType.Int32, enable)
			};
			return this.conn.Execute("INSERT INTO [CM_Advertisement]\r\n                                   ([AdName]\r\n                                   ,[AdLocation]\r\n                                   ,[ImgPath]\r\n                                   ,[AdIntro]\r\n                                   ,[LinkUrl]\r\n                                   ,[LinkTip]\r\n                                   ,[Province]\r\n                                   ,[City]\r\n                                   ,[Area]\r\n                                   ,[SortClassID]\r\n                                   ,[Enabled]\r\n                                   ,[BeginTime]\r\n                                   ,[EndTime])\r\n                             VALUES\r\n                                   (\r\n                                   @AdName,\r\n                                   @AdLocation,\r\n                                   @ImgPath,\r\n                                   @AdIntro,\r\n                                   @LinkUrl,\r\n                                   @LinkTip,\r\n                                   @Province,\r\n                                   @City,\r\n                                   @Area,\r\n                                   @SortClassID,\r\n                                   @Enabled,\r\n                                   @BeginTime,\r\n                                   @EndTime\r\n                                   )", parameters) > 0;
		}
		public DataTable GetAdByID(int ad_id)
		{
			return this.conn.GetDataTable("select *  from CM_Advertisement where AdID=" + ad_id.ToString() + " and IsDel=1 ");
		}
		public DataTable GetAllAd(int PageSize, int CurrentPage, string queString)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@PageSize", DbType.Int32, PageSize),
				this.conn.CreateParameter("@CurrentPage", DbType.Int32, CurrentPage)
			};
			return this.conn.GetDataTable("select * from \r\n                                        (select *,\r\n                                        case \r\n                                        when Enabled=0 then 0\r\n                                        when EndTime<GETDATE() then 0\r\n                                        else 1\r\n                                        end IsEnable,\r\n                                        ROW_NUMBER() OVER(ORDER BY  InputTime Desc) AS number\r\n                                         from CM_Advertisement where \r\n                                        AdLocation!='版位一' and AdLocation!='版位二' and AdLocation!='版位三' and AdLocation!='版位四'\r\n                                        and IsDel=1 " + queString + " ) t\r\n                                        where t.number>(@CurrentPage-1)*@PageSize and t.number<=@CurrentPage*@PageSize", parameters);
		}
	}
}
